/*
 * @Description: 封装的一些上传方法。注释为查看原代码之后，自己的理解
 */
import globalMethods from '@/plugins/methods';
import {useBaseInfoWithOut} from "@/store/modules/base-info";

/**
 * @description: 上传之前检查是否需要选择密级、以及是否选了密级
 * @param {Array} files 文件列表
 * @param {Boolean} isSetSecretLevel 是否需要密级
 * @return {Promise}返回一个promise。需要选择密级，resolve的就是这事了选中密级之后的文件列表
 */
export const checkSecret = (files, isSetSecretLevel) =>
    new Promise((resolve, reject) => {
        console.log(files);

        //需要验证密级
        if (isSetSecretLevel) {
          const useBaseInfo = useBaseInfoWithOut();
            //弹出密级选择框
            var secretLevelDialog = globalMethods.dialog({
                id: 'secretLevelDialog',
                width: 560,
                height: 401,
                title: $.i18n('secretlevel.page.title'),
                url: `${_ctxPath}/apps_res/common/attSecretUI4.html`,
                transParams: {
                  files,
                  secretLevel: useBaseInfo.baseInfo.secretLevel.value
                },
                bodyStyle: {
                   
                    padding:'0'
                },
                onCancel() {
                    reject();
                    secretLevelDialog.close();
                },
                onConfirm() {
                    var returnValue = secretLevelDialog.getValue();
                    // 设置了密级，就依次把字段设置回来
                    if (returnValue) {
                        files.forEach((file) => {
                            const item = returnValue.find((d) => d.fileName === file.name);
                            if (item) {
                                file.secretLevel = item.secretLevel;
                                file.secretLevelName = item.secretLevelName;
                            }
                        });
                        resolve(files);
                    } else {
                        //必须要选密级
                        reject();
                    }
                    secretLevelDialog.close();
                },
            });
        } else {
            resolve(files);
        }
    });

/**
 * @description: 上传之前检查文件状态
 * @param {Promise}api 检查状态的请求
 * @param {Array}fileList 检查状态的文件列表
 * @return {Promise}
 */
export const checkFileStatus = (api, breakpointId, fileList) =>
    new Promise((resolve, reject) => {
        const fbArray = fileList.map((file) => ({
            lastModifiedDate: globalMethods.getFileLastModifiedDate(file).toString(),
            fileName: encodeURIComponent(file.name),
            fileSize: file.size.toString(),
            secretLevel: file.secretLevel,
            secretLevelName: file.secretLevelName,
        }));
        //保持以前的逻辑
        const config = {};
        const csrfToken = CsrfGuard.getToken();
        if (csrfToken) {
            config.headers = Object(config.headers, {
                'X-Requested-With': 'XMLHttpRequest',
                CSRFTOKEN: csrfToken,
            });
        }

        //发送请求
        api(
            `managerMethod=getUploadFilesStartIndex&arguments=${JSON.stringify([
                fbArray,
                breakpointId,
            ])}`,
            config
        )
            .then((res) => {
                if (res && res.length) {
                    res.forEach((d) => {
                        //如果有断点续传，就设置起始点
                        if (d.start !== '-1') {
                            fileList.find((file) => file.name === d.fileName).startIndex = +d.start;
                        }
                    });
                    resolve();
                } else {
                    reject();
                }
            })
            .catch(reject);
    });

/**
 * @description: 上传开始，添加临时文件
 * @param {Array}list 待添加列表
 * @param {Object}file 待添加文件
 * @return {*}
 */
export const addUploadingAttachment = (list, file) => {
    list.push(file);
};

/**
 * @description: 上传失败，删除当前文件
 * @param {Array}list 待删除列表
 * @param {String}fileUrl 待删除文件的fileUrl，其实就是id
 * @return {*}
 */
export const deleteUploadingAttachment = (list, fileUrl) => {
    //正在上传的这一个
    const index = list.findIndex((d) => d.isUploading && d.fileUrl === fileUrl);
    if (index !== -1) {
        list.splice(index, 1);
    }
};
